package thread.processing;

import static java.lang.System.out;

/**
 * This class is the same version as PingPong3 but this time we have added
 * start() and stop() methods for convenience.
 * 
 * @author Shahid
 */
public class ThreadBadStop implements Runnable {

	String word;
	int delay;
	volatile Thread This_Thread = null;

	public ThreadBadStop(String w, int d) {
		word = w;
		delay = d;
		out.println("Sleep time of " + word + " = " + delay);
	}

	public void start() {
		if (This_Thread == null) {
			This_Thread = new Thread(this);
			This_Thread.start();
		}
	}

	public void stop() {
		if (This_Thread != null)
			This_Thread = null;
	}

	public void run() {
		try {
			for (int count = 1; count < 10; count++) {
				out.printf("%s%d\t", word, count);
				Thread.sleep(delay);
			}
		} catch (InterruptedException e) {
			out.println(e);
		}
	}

	public static void main(String[] args) {
		out.println("Ping Pong Thread demo");

		ThreadBadStop ping = 
				new ThreadBadStop("Ping", 500);
		
		ping.start();
		ping.stop();
		
		new ThreadBadStop("Pong", 0).start();
	}
}

// Ping Pong Thread demo 3
// Sleep time of Ping = 500
// Sleep time of Pong = 0
// Pong1 Ping1 Pong2 Pong3 Pong4 Pong5 Pong6 Pong7 Pong8 Pong9 Ping2 Ping3 Ping4
// Ping5 Ping6 Ping7 Ping8 Ping9
